home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / UNKEYWORDINDEX.PY < prev    next >
Encoding:
Python Source  |  2000-11-13  |  1.9 KB  |  72 lines

  1. from UnIndex import UnIndex, MV, intSet
  2. from types import ListType, TupleType
  3. from zLOG import LOG, ERROR
  4.  
  5. class UnKeywordIndex(UnIndex):
  6.  
  7.     meta_type = 'Keyword Index'
  8.     
  9.     """Like an UnIndex only it indexes sequences of items
  10.     
  11.     Searches match any keyword.
  12.  
  13.     This should have an _apply_index that returns a relevance score
  14.     """
  15.  
  16.     def index_object(self, i, obj, threshold=None):
  17.         """ index an object 'obj' with integer id 'i'"""
  18.  
  19.         # Before we do anything, unindex the object we've been handed, as
  20.         # we can't depend on the user to do the right thing.
  21.         self.unindex_object(i)
  22.  
  23.         index = self._index
  24.         unindex = self._unindex
  25.  
  26.         id = self.id
  27.  
  28.         try:
  29.             kws=getattr(obj, id)
  30.             if callable(kws):
  31.                 kws = kws()
  32.         except:
  33.             kws = [MV] 
  34.         
  35.         # index each item in the sequence
  36.         for kw in kws:
  37.             set = index.get(kw)
  38.             if set is None:
  39.                 index[kw] = set = intSet()
  40.             set.insert(i)
  41.         
  42.         unindex[i] = kws
  43.  
  44.         self._index = index
  45.         self._unindex = unindex
  46.  
  47.         return 1
  48.     
  49.  
  50.     def unindex_object(self, i):
  51.         """ carefully unindex the object with integer id 'i' and do not
  52.         fail if it does not exist """
  53.         index = self._index
  54.         unindex = self._unindex
  55.  
  56.         kws = unindex.get(i, None)
  57.         if kws is None:
  58.             return None
  59.         for kw in kws:
  60.             set = index.get(kw, None)
  61.             if set is not None:
  62.                 set.remove(i)
  63.             else:
  64.                 LOG('UnKeywordIndex', ERROR, ('unindex_object could not '
  65.                                               'remove %s from set'
  66.                                               % str(i)))
  67.         del unindex[i]
  68.         
  69.         self._index = index
  70.         self._unindex = unindex
  71.  
  72.